//
//  SettingViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2016/12/5.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "SettingViewController.h"
#import "UserModel.h"
#import "SettingTableViewCell.h"
#import "DeviceProfileViewController.h"
#import "InfoSettingViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *masterControlNumberLabel;
@property (strong, nonatomic) UserModel *userModel;
@property (weak, nonatomic) IBOutlet UITableView *deviceListTableView;

@end

@implementation SettingViewController

-(UserModel *)userModel{
    
    if (!_userModel) {
        _userModel = [[UserModel alloc]init];
    }
    
    return _userModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.masterControlNumberLabel setText:self.userModel.userName];
    
    self.deviceListTableView.delegate = self;
    self.deviceListTableView.dataSource = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.userModel.deviceByAccountArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.userModel.deviceByAccountArray[indexPath.row];
    
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingTableViewCell"];
    
    if (cell == nil) {
        
        cell = [[SettingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingTableViewCell"];
        
    }
    
    //设置deviceName
    if ([self.userModel.changeDeviceNameDictionary valueForKey:[dict valueForKey:NC_SERIAL_NUMBER]]) {
        
        [cell setUpCellForDeviceName:[self.userModel.changeDeviceNameDictionary valueForKey:[dict valueForKey:NC_SERIAL_NUMBER]]];
        
    }else{
        
        [cell setUpCellForDeviceName:[dict valueForKey:NC_DISPLAY_NAME]];
        
    }
    
    
    
    cell.serialNumber = [dict valueForKey:NC_SERIAL_NUMBER];
    
    UIImage *image = [self.userModel.deviceIconDictionary valueForKey:[dict valueForKey:NC_SERIAL_NUMBER]];
    
    [cell setUpIconForImage:image];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

#pragma mark 刷新
-(void)reloadTableView{
    
    [self.deviceListTableView reloadData];
    
}

#pragma mark 点击跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.userModel.deviceByAccountArray[indexPath.row];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SettingViewController" bundle:[NSBundle mainBundle]];
    
    DeviceProfileViewController *deviceProfileViewController = [storyboard instantiateViewControllerWithIdentifier:@"DeviceProfileViewController"];
    
    NSString *deviceName = [dict valueForKey:NC_DISPLAY_NAME];
    
    NSString *serialNumber = [dict valueForKey:NC_SERIAL_NUMBER];
    
    NSLog(@"serialNumber %@ deviceName %@",serialNumber,deviceName);
    
    [deviceProfileViewController setUpInfoForSerialNumber:serialNumber DeviceName:deviceName];
    
    [deviceProfileViewController setUpIconForImage:[self.userModel.deviceIconDictionary valueForKey:serialNumber]];
    
    [self showViewController:deviceProfileViewController sender:nil];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
